package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class ConfirmOrderFormServlet extends HttpServlet {
    private static final String CONFIRMORDER_FORM= "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    private static final String SHIPPING_FORM= "/WEB-INF/jsp/order/ShippingForm.jsp";
    private static final String VIEW_SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";

    private Order order = null;
    private Cart cart = null;
    private String shippingAddressRequired;
    private boolean confirmed;
    private List<Order> orderList;
    private OrderService orderService;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        HttpSession session = req.getSession();

        order = (Order)session.getAttribute("order");
        cart = (Cart)session.getAttribute("cart");
        Account account = (Account)session.getAttribute("account");
        if (account == null ) {
            session.setAttribute("message","You must sign on before attempting to check out.  Please sign on and try checking out again.");
            req.getRequestDispatcher(VIEW_SIGNON).forward(req,resp);
        }
        else {

            //下面语句的着各种情况暂时不管
            //session.setAttribute("message","An order could not be created because a cart could not be found.");

            shippingAddressRequired = req.getParameter("shippingAddressRequired");

            session.setAttribute("shippingAddressRequired",shippingAddressRequired);


            if (shippingAddressRequired != null){





                order.setBillAddress1(req.getParameter("order.billAddress1"));
                order.setBillAddress2(req.getParameter("order.billAddress2"));
                order.setBillZip(req.getParameter("order.billZip"));
                order.setBillToFirstName(req.getParameter("order.billToFirstName"));
                order.setBillToLastName(req.getParameter("order.billToLastName"));
                order.setBillCity(req.getParameter("order.billCity"));
                order.setBillCountry(req.getParameter("order.billCountry"));
                order.setBillState(req.getParameter("order.billState"));

                order.setCreditCard(req.getParameter("order.creditCard"));
                order.setCardType(req.getParameter("order.cardType"));




                session.setAttribute("order",order);

                req.getRequestDispatcher(SHIPPING_FORM).forward(req,resp);

            }
            else {

                order = (Order)session.getAttribute("order");




            order.setBillAddress1(req.getParameter("order.billAddress1"));
            order.setBillAddress2(req.getParameter("order.billAddress2"));
            order.setBillZip(req.getParameter("order.billZip"));
            order.setBillToFirstName(req.getParameter("order.billToFirstName"));
            order.setBillToLastName(req.getParameter("order.billToLastName"));
            order.setBillCity(req.getParameter("order.billCity"));
            order.setBillCountry(req.getParameter("order.billCountry"));
            order.setBillState(req.getParameter("order.billState"));

            order.setCreditCard(req.getParameter("order.creditCard"));
            order.setCardType(req.getParameter("order.cardType"));




                session.setAttribute("order",order);




                account = (Account)session.getAttribute("account");
                if(account != null){
                    HttpServletRequest httpRequest= req;
                    String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                    LogService logService = new LogService();

                    String logInfo = logService.logInfo(" ") + strBackUrl + " 确定订单";
                    logService.insertLogInfo(account.getUsername(), logInfo);
                }



                req.getRequestDispatcher(CONFIRMORDER_FORM).forward(req,resp);
            }

        }



    }
}
